new Vue({
    el: "#app",
    data: {
        activeName: 'first',
        dialogVisible: false,
        tableData: [],
        search: '',
        dialogData: {},
        currentPage: 1,
        currentPage1: 1,
        currentPage2: 1,
        currentPage3: 1,
        total: 0,
        total1: 0,
        total2: 0,
        total3: 0,
        // pages: 0
        visible: false,
        userRole: '',
        articleData: [],
        commentData: [],
        imageUrl: '',
        userNickname: '',
        orderData: [],
        loading1: true,
        loading2: true,
        loading3: true,
        loading4: true,
        url_treeHole: '',
        userId: '',

    },
    methods: {

        getArticleByJsoup() {
            if (this.url_treeHole.length <= 0) {
                this.$message.error("不能为空");
                return;
            }
            let params = new URLSearchParams();
            params.append("op", "getArticleByURL");
            params.append("url", this.url_treeHole);
            params.append("userId", this.userId);
            axios.post('/treeHole/jsoup.action', params).then(res => {
                if (res.data.code == '200') {
                    this.$message.success("导入成功");
                } else {
                    this.$message.info("此链接已导入，请务再导入");
                }
            }).catch(res => {
                this.$message.error("导入失败");
            })
        },

        handleEdit(index, row) {
            console.log(index, row);
        },
        handleDelete(index, row) {
            console.log(index, row);
        },
        //打开广告
        open1() {
            const h = this.$createElement;

            this.$notify({
                title: '管理员须知',
                message: h('i', {
                    style: 'color: teal'
                }, '请各位管理员谨慎删除')
            });
        },

        go() {
            this.$message.info("此功能暂时取消");
        },

        logout() {
            let params = new URLSearchParams()
            params.append("op", "adminLoginOut");
            axios.post('user.action', params).then(res => {
                if (res.data.code = 200) {
                    window.location.href = "admin-login.html";
                    this.$message.success("退出成功");
                } else {
                    this.$message.error(res.data.message);
                }
            })

        },
        management(e) {
            let index = e.index
            //用户
            if (index == 0) {
                this.getAllUser();
            }
            //文章
            if (index == 1) {
                this.handleCurrentChange2(1);
            }
            //评论
            if (index == 2) {
                this.handleCurrentChange3(1);
            }
            //赞赏管理
            if (index == 3) {
                this.handleCurrentChange4(1);
            }

        },


        handleClick(index, row) {

            this.dialogVisible = true;
            this.dialogData.userId = row.userId;
            this.dialogData.userName = row.userName;
            this.dialogData.userEmail = row.userEmail;
            this.dialogData.userRole = row.userRole;

            if (this.dialogData.userRole == 'admin') {
                this.userRole = '管理员'
            } else {
                this.userRole = '普通用户'
            }
        },

        filterTag(value, row) {
            return row.userRole === value;
        },
        filterTag2(value, row) {
            return row.orderStatus === value;
        },
        changeAdmin() {
            this.dialogData.userRole = this.userRole
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getAllUser();
        },
        handleCurrentChange2(val) {
            this.currentPage1 = val;
            let params = new URLSearchParams();
            params.append("op", "getAllArticle");
            params.append("curPage", this.currentPage1);
            axios.post('article.action', params).then(res => {
                this.articleData = res.data.data.article;
                this.articleData.forEach(element => {
                    element.simpleComtent = element.articleContent.substring(0, 50) + " ...";
                });
                this.total1 = res.data.data.total;
            })
        },

        handleCurrentChange3(val) {
            this.currentPage2 = val;
            let params = new URLSearchParams();
            params.append("op", "getAllComments");
            params.append("curPage", this.currentPage2);
            axios.post('comment.action', params).then(res => {
                this.commentData = res.data.data.comments;
                this.commentData.forEach(element => {
                    element.simpleComtent = element.commentContent.substring(0, 50) + " ...";
                });
                this.total2 = res.data.data.total;
            })
        },
        handleCurrentChange4(val) {
            this.currentPage3 = val;
            let params = new URLSearchParams();
            params.append("op", "getAllOrder");
            params.append("curPage", this.currentPage3);
            axios.post('pay.action', params).then(res => {
                this.orderData = res.data.data.orders;
                this.total3 = res.data.data.total;
            })
        },

        modifyUserInfo() {
            let params = new URLSearchParams();
            params.append("op", "updateUser");
            params.append("userId", this.dialogData.userId);
            params.append("userName", this.dialogData.userName);
            params.append("userEmail", this.dialogData.userEmail);
            params.append("userRole", this.dialogData.userRole);
            axios.post('user.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.data.message);
                } else {
                    this.$message.success("修改成功");
                    this.getAllUser();
                }
            })
            this.dialogVisible = false
        },
        changeInput() {
            this.$forceUpdate()
        },

        handleDelete(index, row) {
            this.$refs[row.userId].doClose()
            let params = new URLSearchParams();
            params.append("op", "deleteArticle");
            params.append("userId", row.userId);
            axios.post('user.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.data.message);
                } else {
                    this.$message.success("删除成功");
                    this.getAllUser();
                }
            })
        },
        handleDelete2(index, row) {
            this.$refs[row.articleId].doClose()
            let params = new URLSearchParams();
            params.append("op", "deleteArticle");
            params.append("articleId", row.articleId);
            axios.post('article.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.data.message);
                } else {
                    this.$message.success("删除成功");
                    this.handleCurrentChange2(this.currentPage1);
                }
            })
        },

        handleDelete3(index, row) {

            this.$refs[row.commentId].doClose()
            let params = new URLSearchParams();
            params.append("op", "deleteComment");
            params.append("commentId", row.commentId);
            axios.post('comment.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.data.message);
                } else {
                    this.$message.success("删除成功");
                    this.handleCurrentChange3(this.currentPage2);
                }
            })
        },
        handleDelete4(index, row) {
            this.$refs[row.orderId].doClose()
            let params = new URLSearchParams();
            params.append("op", "deleteOrder");
            params.append("orderId", row.orderId);
            axios.post('pay.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.data.message);
                } else {
                    this.$message.success("删除成功");
                    this.handleCurrentChange4(this.currentPage3);
                }
            })
        },

        deleteTimeOutOrder() {
            axios.post('http://127.0.0.1:7000/deleteTimeOutOrder').then(res => {
                if (res.data.status == "500") {
                    this.$message.error(res.data.data.errorMsg);
                } else {
                    this.$message.success("删除成功");
                    let params = {
                        curPage: this.currentPage3
                    }
                    axios.post('http://127.0.0.1:7000/getAllOrder', params).then(res => {
                        this.orderData = res.data.data.orders;
                        this.total3 = res.data.data.total;
                    })
                }
            })
        },


        //--------页面加载事件--------
        getAllUser() {
            let params = new URLSearchParams()
            params.append("op", "getAllUser");
            params.append("curPage", this.currentPage);
            axios.post('user.action', params).then(res => {
                res = res.data.data;
                this.total = res.total;
                this.tableData = res.user;
                this.tableData.forEach(element => {
                    element.visible = false
                });
            })
        },
        checkIsAdmin() {
            let params = new URLSearchParams()
            params.append("op", "getAdmin");
            axios.post('user.action', params).then(res => {
                if (res.data.code == 500) {
                    this.$message.error(res.data.message);
                    window.location.href = "admin-login.html";
                } else {
                    this.imageUrl = res.data.data.user.userProfilePhoto;
                    this.userNickname = res.data.data.user.userName;
                    this.userId = res.data.data.user.userId;
                }
            })
        }
    },
    created() {
        this.checkIsAdmin();
        this.getAllUser();
        this.open1();
    }
});